package com.xxxx.sink_operator;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.core.fs.FileSystem;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;
import java.util.List;

/**
 * @program: flink19Test
 * @description:
 * @author: CoreDao
 * @create: 2021-04-01 17:06
 **/

public class SinkWriteAsText {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


//        env.setParallelism(4);
        env.setParallelism(1);

        List<String> list = Arrays.asList("hello nihao", "hello nihao1", "hello nihao2","hello nihaos");

        DataStreamSource<String> dataStreamSource = env.fromCollection(list);
        //并行度 =1，就是目录和文件名，如果并行度>1那么产生的目录下会有对应id的文件
        DataStreamSink<String> sink = dataStreamSource.writeAsText("./data/write/Text.txt", FileSystem.WriteMode.OVERWRITE);

        SingleOutputStreamOperator<Tuple2<String, Integer>> map = dataStreamSource.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                return Tuple2.of(value, 1);
            }
        });
        map.writeAsCsv("./data/write/Csv.csv", FileSystem.WriteMode.OVERWRITE);


        env.execute();
    }
}
